<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="90px">
      <el-form-item label="企业名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerId">
        <matic-select v-model:value="queryParams.customerId" :url="`customer/getAllList/forManager`"
          :objMap="{ value: 'id', label: 'title' }" placeholder="请选择" />
      </el-form-item>
      <!-- <el-form-item label="地区" prop="address">
        <area-select type="tree" v-model:value="queryParams.address"></area-select>
      </el-form-item> -->
      <el-form-item label="地区" prop="address">
        <area-select v-model:provinceId="queryParams.provinceId" v-model:cityId="queryParams.cityId"
          v-model:districtId="queryParams.districtId"></area-select>
      </el-form-item>
      <el-form-item v-show="false" prop="cityId"></el-form-item>
      <el-form-item v-show="false" prop="provinceId"></el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <page-table :url="url.list" ref="table" :tableForm="queryParams" :columns="columns">
      <template #action="scope">
        <el-tooltip content="修改" placement="top">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
        </el-tooltip>
      </template>
      <template #status="scope">
        <el-switch v-model="scope.row.status" v-if="scope.row.status != undefined" :active-value="1" :inactive-value="0"
          @change="handleDelete(scope.row, scope.row.status)"></el-switch>
      </template>

      <template #address="scope">
        {{ scope.row.province + scope.row.city + scope.row.district }}
      </template>
      <template #electricityTypeStr="scope">
        大工业
      </template>
      <template #customerTypeStr="scope">
        公线专变客户
      </template>
      <template #voltageLevelStr="scope">
        110kv
      </template>
      <template #measurementMethodStr="scope">
        高供高计
      </template>
      <template #industry="scope">
        商业
      </template>
    </page-table>

    <!-- 添加或修改企业配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="企业名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerId">
          <matic-select v-model:value="form.customerId" :url="`customer/getAllList/forManager`"
            :objMap="{ value: 'id', label: 'title' }" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="地区" prop="districtId">
          <area-select v-model:provinceId="form.provinceId" v-model:cityId="form.cityId"
            v-model:districtId="form.districtId"></area-select>
        </el-form-item>
        <el-form-item label="行业类型" prop="industryId">
          <matic-select v-model:value="form.industryId" :options="[
            { value: '1', label: '商业' }
          ]" :objMap="{ value: 'value', label: 'label', children: 'children' }" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="电力类型" prop="electricityTypeId">
          <matic-select v-model:value="form.electricityTypeId" :options="[
            { value: '1', label: '大工业' }
          ]" :objMap="{ value: 'value', label: 'label', children: 'children' }" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="客户类型" prop="customerTypeId">
          <matic-select v-model:value="form.customerTypeId" :options="[
            { value: '1', label: '公线专变客户' }
          ]" :objMap="{ value: 'value', label: 'label', children: 'children' }" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="电压等级" prop="voltageLevelId">
          <matic-select v-model:value="form.voltageLevelId" :options="[
            { value: '1', label: '110kv' }
          ]" :objMap="{ value: 'value', label: 'label', children: 'children' }" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="测量方法" prop="measurementMethodId">
          <matic-select v-model:value="form.measurementMethodId" :options="[
            { value: '1', label: '高供高计' }
          ]" :objMap="{ value: 'value', label: 'label', children: 'children' }" placeholder="请选择" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


  </div>
</template>

<script setup name="Role">
import request from '@/utils/request'
const router = useRouter();
const { proxy } = getCurrentInstance();

const url = ref({
  list: 'company/getPage/forManager',
  fine: 'company/getOne/forManager',
  delete: 'company/disableOrUse/forManager',
  add: 'company/save/forManager',
  edit: 'company/save/forManager'
});

const Customerurl = ref({
  fine: 'users/getCustomerDataAuth/forManager',
  edit: 'users/saveCustomerDataAuth/forManager'
});

const open = ref(false);
const title = ref("");
const openDataScope = ref(false);
const table = ref(null);

const columns = ref([{
  key: 'title',
  title: '企业名称',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'customer',
  title: '客户名称',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'address',
  title: '地区',
  ellipsis: true,
  tooltip: true,
  slots: {
    customKey: 'address',
  },
},
{
  key: 'electricityTypeStr',
  title: '电力类型',
  ellipsis: true,
  tooltip: true,
  slots: {
    customKey: 'electricityTypeStr',
  },
},
{
  key: 'customerTypeStr',
  title: '客户类型',
  ellipsis: true,
  tooltip: true,
  slots: {
    customKey: 'customerTypeStr',
  },
},
{
  key: 'voltageLevelStr',
  title: '电压等级',
  ellipsis: true,
  tooltip: true,
  slots: {
    customKey: 'voltageLevelStr',
  },
},
{
  key: 'measurementMethodStr',
  title: '测量方法',
  ellipsis: true,
  tooltip: true,
  slots: {
    customKey: 'measurementMethodStr',
  },
},
{
  key: 'industry',
  title: '行业类型',
  ellipsis: true,
  tooltip: true,
  slots: {
    customKey: 'industry',
  },
},
{
  key: 'devicesCount',
  title: '电表数量',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'status',
  title: '状态',
  align: 'center',
  width: 150,
  slots: {
    customKey: 'status',
  },
},
{
  title: '操作',
  key: 'action',
  slots: {
    customKey: 'action',
  },
  align: 'center',
  fixed: 'right',
  width: 150
}]);

const handleDelete = (row, status) => {
  const id = row.id
  let text = row.status == "1" ? "启用" : "停用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.title + '"吗?').then(function () {
    return request.get(url.value.delete, { params: { id } });
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status === "0" ? "1" : "0";
  });
}

/** 数据范围选项*/
const dataScopeOptions = ref([
  { value: "1", label: "全部数据权限" },
  { value: "2", label: "自定数据权限" },
]);

const data = reactive({
  form: {},
  queryParams: {},
  rules: {
    title: [{ required: true, message: "企业名称不能为空", trigger: "blur" }],
    customerId: [{ required: true, message: "客户名称不能为空", trigger: "blur" }],
    industryId: [{ required: true, message: "行业类型不能为空", trigger: "blur" }],
    electricityTypeId: [{ required: true, message: "电力类型不能为空", trigger: "blur" }],
    customerTypeId: [{ required: true, message: "客户类型不能为空", trigger: "blur" }],
    voltageLevelId: [{ required: true, message: "电压等级不能为空", trigger: "blur" }],
    measurementMethodId: [{ required: true, message: "测量方法不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  table.value.onload();
}
/** 搜索按钮操作 */
function handleQuery() {
  table.value.init();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    title: '',
    customerId: '',
  };
  proxy.resetForm("roleRef");
}
/** 添加企业 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加企业";
}
/** 修改企业 */
function handleUpdate(row) {
  reset();
  const id = row.id
  request.get(url.value.fine, { params: { id } }).then(({ result }) => {
    form.value = {
      ...result,
      provinceId: String(result.provinceId),
      cityId: String(result.cityId),
      customerId: String(result.customerId),
      districtId: String(result.districtId),

    };
    open.value = true;
    title.value = "修改企业";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        request.post(url.value.edit, form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        request.post(url.value.add, form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}


</script>
